package amdocs.jet.pm.xml;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import amdocs.jet.pm.Constants;
import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Xml;

public class AndroidSaxCurrencyParser implements CurrencyParser 
{
	public AndroidSaxCurrencyParser() 
	{
		super();
	}

	public AllCurrencies parse(InputStream currenciesInputStream) 
	{
		final AllCurrencies allCurrencies = new AllCurrencies();
		final ArrayList<Currency> currencyList = new ArrayList<Currency>();
		final Currency currentCurrency = new Currency();

		RootElement root = new RootElement(Constants.CURRENCIES);
		Element item = root.getChild(Constants.CURRENCY);
		root.getChild(Constants.REQUESTED_DATE).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				allCurrencies.setRequestedDate(body);
			}
		});
		root.getChild(Constants.LAST_UPDATE).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				allCurrencies.setLastUpdate(body);
			}
		});
		root.getChild(Constants.ERROR1).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				allCurrencies.setError1(body);
			}
		});
		root.getChild(Constants.ERROR2).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				allCurrencies.setError2(body);
			}
		});
		root.getChild(Constants.ERROR3).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				allCurrencies.setError3(body);
			}
		});
		
		
		item.setEndElementListener(new EndElementListener(){
			public void end() {
				currencyList.add(currentCurrency.copy());
			}
		});
		item.getChild(Constants.NAME).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentCurrency.setName(body);
			}
		});
		item.getChild(Constants.UNIT).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentCurrency.setUnit(body);
			}
		});
		item.getChild(Constants.CURRENCYCODE).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentCurrency.setCurrencyCode(body);
			}
		});
		item.getChild(Constants.COUNTRY).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentCurrency.setCountry(body);
			}
		});
		item.getChild(Constants.RATE).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentCurrency.setRate(body);
			}
		});
		item.getChild(Constants.CHANGE).setEndTextElementListener(new EndTextElementListener(){
			public void end(String body) {
				currentCurrency.setChange(body);
			}
		});
		try {
			Xml.parse(currenciesInputStream, Xml.Encoding.UTF_8, root.getContentHandler());
		} catch (Exception e) {
			System.err.println(e.getMessage());
			e.printStackTrace();
		}
		allCurrencies.setCurrencies(currencyList);
		return allCurrencies;
	}
}
